import '../utils/tools.dart';

import 'comparable.dart';
import 'serializable.dart';

class ArticleCommentModel extends Serializable implements Equable<int> {
  int id = 0;
  int articleId = 0;
  int channelId = 0;
  int memberId = 0;

  int replyId = 0;
  int groupId = 0;

  String nickName = '';
  String avatar = '';
  String email = '';

  int createTime = 0;
  int status = 0;

  bool isAnonymous = false;
  String content = '';

  String device = '';
  String ip = '';

  bool _isSubmit = false;
  ArticleCommentModel({
    required this.articleId,
    required this.channelId,
    required this.memberId,
    this.replyId = 0,
    this.isAnonymous = false,
  }) : _isSubmit = true;

  ArticleCommentModel.fromJson(Map? model) {
    if (model == null || !model.containsKey('id')) return;

    id = Tools.parseInt(model['id']);
    articleId = model['article_id'] ?? 0;
    channelId = model['channel_id'] ?? 0;
    memberId = model['member_id'] ?? 0;
    nickName = model['nickname'] ?? '';
    avatar = model['avatar'] ?? '';
    email = model['email'] ?? '';

    content = model['content'] ?? '';

    isAnonymous = Tools.parseBool(model['is_anonymous']);
    replyId = model['reply_id'] ?? 0;
    groupId = model['group_id'] ?? 0;
    status = model['status'] ?? 0;
    createTime = model['create_time'] ?? 0;

    device = model['device'] ?? '';
    ip = model['ip'] ?? '';
  }

  @override
  Map<String, dynamic> toJson() {
    Map<String, Object> jsonObj = {};
    jsonObj['member_id'] = memberId;
    jsonObj['article_id'] = articleId;
    jsonObj['channel_id'] = channelId;
    jsonObj['reply_id'] = replyId;
    jsonObj['email'] = email;
    jsonObj['content'] = content;
    jsonObj['is_anonymous'] = isAnonymous;
    if (!_isSubmit) {
      jsonObj['id'] = id;
      jsonObj['create_time'] = createTime;
      jsonObj['device'] = device;
      jsonObj['ip'] = ip;
      jsonObj['status'] = status;
    }
    return jsonObj;
  }

  @override
  int get compareField => id;
}
